/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
/*  var getIntersectionNode = function(headA, headB) {
    let set=new Set(); 
    while(headA!=null || headB!=null){
        if(headA!=null){
            if(set.has(headA)){
                return headA;
            }
            set.add(headA);
            headA=headA.next;
        }
        //if(headA!=null)
        else{
            if(set.has(headB)){
                return headB;
            }
            set.add(headB);
            headB=headB.next;
        }
    }
    return null;
}; */
/* var getIntersectionNode = function(headA, headB) {
    let set=new Set(); 
    while(headA!=null){
            set.add(headA);
            headA=headA.next;
        }
    while(headB!=null){
            if(set.has(headB)){
                return headB;
            }
            set.add(headB);
            headB=headB.next;
        }
    return null;
};  */
var getIntersectionNode = function(headA, headB) {
    let nodeA=headA;
    let nodeB=headB;
    while(nodeA!=nodeB){
            nodeA=nodeA == null ? headB : nodeA.next;
            nodeB=nodeB == null ? headA : nodeB.next;
        }
    return nodeA;
}; 